package com.kanas.webak.organization.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.inject.Singleton;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kanas.framework.business.BusinessException;
import com.kanas.webak.organization.domain.Calendar;
import com.kanas.webak.organization.domain.Organization;
import com.kanas.webak.organization.repository.CalendarRepository;
import com.kanas.webak.organization.repository.OrganizationRepository;

@Service("com.kanas.webak.organization.service.calendarservice")
@Singleton
public class CalendarServiceImpl implements CalendarService{
	
	@Autowired
	private CalendarRepository calendarRepository;
	@Autowired 
	private OrganizationRepository organizationRepository;
	
	@Override
	@Transactional
	public String addNew(Calendar calendar, String oid ,String type) throws BusinessException {
		if(type.equals("rule")){
			Organization o = null;
			if(oid != null){
				o = organizationRepository.findOne(oid);
			}
			calendar.setOrganization(o);
			calendar.setRuleFlag(true);
			calendar.setWorkdayFlag(false);
			Integer maxIndex = calendarRepository.findMaxSortNumber().get(0);
			if(maxIndex != null && maxIndex != 0){
				calendar.setSortNumber(maxIndex+1);
			}else{
				calendar.setSortNumber(1);
			}
			calendarRepository.save(calendar);
		} else {
			Organization o = null;
			if(oid != null){
				o = organizationRepository.findOne(oid);
			}
			calendar.setOrganization(o);
			calendar.setRuleFlag(false);
			
			if(calendarRepository.findSchedule(o, calendar.getBeginDate(), calendar.getEndDate(), calendar.getBeginDate(), calendar.getEndDate(), false).size() != 0){
				throw BusinessException.DATA_DUPLICATION;
			} else {
				calendarRepository.save(calendar);
			}
			
		}
			
		return calendar.getId();
	}
	
	@Override
	@Transactional
	public Page<Calendar> findByOrg(Pageable pageable, String orgId) {
		Organization o = organizationRepository.findOne(orgId);
		return calendarRepository.findByOrganizationAndSortNumberNotNullOrderBySortNumberDesc(o, pageable);
	}
	
	@Override
	@Transactional
	public Page<Calendar> findByOrgAndYear(Pageable pageable, String orgId, String year) {
		Organization o = organizationRepository.findOne(orgId);
		Integer yearLater = Integer.parseInt(year)+1;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Date date1 = null; 
		Date date2 = null;
		try {
			date1 = sdf.parse(year);
			date2 = sdf.parse(yearLater.toString());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return calendarRepository.findByOrganizationAndBeginDateBetweenAndSortNumberNotNullAndRuleFlagOrderBySortNumberDesc(o, date1, date2, true, pageable);
	}
	
	@Override
	@Transactional
	public String remove(String ruleId) {
		calendarRepository.delete(ruleId);
		return ruleId;
	}
	
	@Override
	@Transactional
	public Calendar findOne(String cId) {
		return calendarRepository.findOne(cId);
	}
	
	@Override
	@Transactional
	public String edit(Calendar c, String cId) {
		Calendar oldC = calendarRepository.findOne(cId);
		oldC.setAffair(c.getAffair());
		oldC.setBeginDate(c.getBeginDate());
		oldC.setEndDate(c.getEndDate());
		oldC.setFriday(c.getFriday());
		oldC.setMonday(c.getMonday());
		oldC.setSaturday(c.getSaturday());
		oldC.setSunday(c.getSunday());
		oldC.setThursday(c.getThursday());
		oldC.setWednesday(c.getWednesday());
		oldC.setTuesday(c.getTuesday());
		calendarRepository.save(oldC);
		return cId;
	}
	
	@Override
	@Transactional
	public String upSortNum(String oldId) throws Exception {
		Calendar newPc = calendarRepository.findOne(oldId);
		if(newPc==null)throw BusinessException.DATA_NOT_FOUND;
		Integer max = calendarRepository.findMaxSortNumber().get(0);
		if(newPc.getSortNumber()!=max){
			newPc.setSortNumber(max+1);
			calendarRepository.save(newPc);
		}else{
			calendarRepository.save(newPc);
		}
		return oldId;
	}
	
	@Override
	@Transactional
	public String removeItem(String itemId) throws Exception {
		
		Calendar c = calendarRepository.findByIdAndRuleFlag(itemId, false);
		if(c == null) {
			throw new CalendarException("The item is not a schedule");
		} else {
			calendarRepository.delete(c);
		}
		return itemId;
	}
	
	public OrganizationRepository getOrganizationRepository() {
		return organizationRepository;
	}
	public void setOrganizationRepository(
			OrganizationRepository organizationRepository) {
		this.organizationRepository = organizationRepository;
	}
	public CalendarRepository getCalendarRepository() {
		return calendarRepository;
	}
	public void setCalendarRepository(CalendarRepository calendarRepository) {
		this.calendarRepository = calendarRepository;
	}

}
